package tech.jdbc.util;

import java.sql.*;

/**
 * Create with Intellij IDEA
 *
 * @Author Q
 * @date 2021/11/25/14:09
 */
@SuppressWarnings("all")
public class JdbcUtil {

    //方法的好处 - 就是封装代码的细节,提供代码的复用性.
    public static Connection getConnection() throws SQLException {
        String url="jdbc:mysql://localhost:3306/baby";
        String user = "root";
        String password = "root";
        Connection conn = DriverManager.getConnection(url,user,password);
        return conn;
    }

    public static void close(Connection conn, Statement st, ResultSet rs){
        if(null!=rs){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(null!=st){
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(null!=conn){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(Connection conn, Statement st){
        if(null!=st){
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(null!=conn){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
